/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazEditarAsignacionLab.java
 *
 * Created on 26/12/2011, 02:53:49 AM
 */

package presentacion;

import Paneles.PanelGeneral;
import business.AsignaturaBO;
import business.LaboratorioBO;
import business.AreaBO;
import dominio.Area;
import dominio.Asignatura;
import dominio.Laboratorio;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;

/**
 *
 * @author KOFMASTER
 */
public class InterfazEditarAsignacionLab extends javax.swing.JFrame {
    private ImageIcon icono;
    ActionListener accion = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String nombreLabNuevo = ((String)((JComboBox)(e.getSource())).getSelectedItem());
            actualizarTabla(nombreLabNuevo, jTable1.getSelectedRow());
        }
    };

    private void actualizarTabla(String nombreLabNuevo, int filaSeleccionada) {
        AsignaturaBO asignaturaBO = new AsignaturaBO();
        List<Asignatura> asignaturas = asignaturaBO.consultarAsignaturas();
        Asignatura asignatura = new Asignatura();
        LaboratorioBO labBo = new LaboratorioBO();
        Laboratorio laboratorioNuevo = labBo.encontrarLabPorNombre(nombreLabNuevo);
        Laboratorio laboratorioAntiguo = labBo.encontrarLabPorDescripcion((String)jTable1.getValueAt(filaSeleccionada, 3));
        AreaBO areaBO = new AreaBO();
        for (Iterator<Asignatura> it = asignaturas.iterator(); it.hasNext();) {
            Asignatura asignatura1 = it.next();
            if (asignatura1.getIdLaboratorio() == laboratorioAntiguo.getIdLaboratorio()){
                asignatura = asignatura1;
            }
        }
        Area area = (Area) areaBO.consultarArea(asignatura.getIdArea());
        if (nombreLabNuevo.equals("Ninguno")){
            jTable1.setValueAt("---", filaSeleccionada, 3);
            asignatura.setIdLaboratorio(0);
        }
        else{
            jTable1.setValueAt(laboratorioNuevo.getdescripcionLab(), filaSeleccionada, 3);
            asignatura.setIdLaboratorio(laboratorioNuevo.getIdLaboratorio());
        }
        asignaturaBO.actualizarAsignatura(asignatura.getNombreAsignatura(), asignatura.getIdGrado(), asignatura.getNombreAsignatura(), asignatura.getIntensidadHoraria(), asignatura.getIdGrado(), area.getNombreArea());
    }

    /** Creates new form InterfazEditarAsignacionLab */
    public InterfazEditarAsignacionLab() {
        initComponents();
        PanelGeneral panel=new PanelGeneral();
        this.add(panel,BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        iniciarTabla();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setTitle("Edición de Laboratorios");
        setResizable(false);

        jLabel2.setText("Edite la asignacion de laboratorio, seleccionando el laboratorio que desea para cada asignatura:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Asignatura", "Intencidad Horaria", "Laboratorio", "Descripción del Laboratorio"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue("Asignatura");
        jTable1.getColumnModel().getColumn(1).setHeaderValue("Intencidad Horaria");
        jTable1.getColumnModel().getColumn(2).setHeaderValue("Laboratorio");
        jTable1.getColumnModel().getColumn(3).setHeaderValue("Descripción del Laboratorio");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 923, Short.MAX_VALUE)
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazEditarAsignacionLab().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void iniciarTabla() {
        AsignaturaBO asignaturaBo = new AsignaturaBO();
        List<Asignatura> asignaturas = asignaturaBo.consultarAsignaturas();
        List<Asignatura> asignaturasNoAsignadas = asignaturaBo.consultarAsignaturas();
        LaboratorioBO laboratorioBo = new LaboratorioBO();
        List<Laboratorio> laboratorios = laboratorioBo.findAll();
        Laboratorio lab = null;
        int fila = 0;
        int indiceLab = 1;
        for (Iterator<Asignatura> it = asignaturas.iterator(); it.hasNext();) {
            Asignatura asignatura = it.next();
            if (asignatura.getIdLaboratorio() != 0){
                jTable1.setValueAt(asignatura.getNombreAsignatura(), fila, 0);
                jTable1.setValueAt(asignatura.getIntensidadHoraria(), fila, 1);
                JComboBox comboBox = new JComboBox();
                comboBox.addItem("Ninguno");
                jTable1.setValueAt(comboBox, fila, 2);
                asignaturasNoAsignadas.remove(asignatura);
                lab = laboratorioBo.consultarLaboratorio(asignatura.getIdLaboratorio());
                for (Iterator<Laboratorio> it1 = laboratorios.iterator(); it1.hasNext();) {
                    Laboratorio laboratorio = it1.next();
                    if (laboratorio.getdisponibildaLab().equals("SI") && !laboratorio.getnombreLab().equals(lab.getnombreLab())) {
                        comboBox.addItem(laboratorio.getnombreLab());
                        indiceLab++;
                    }
                    else{
                        if (laboratorio.getnombreLab().equals(lab.getnombreLab())){
                            comboBox.addItem(laboratorio.getnombreLab());
                            comboBox.setSelectedItem(indiceLab);
                            jTable1.setValueAt(lab.getdescripcionLab(), fila, 3);
                            indiceLab++;
                        }
                    }
                }
                comboBox.addActionListener(accion);
                fila++;
                indiceLab = 1;
            }
        }
        for (Iterator<Asignatura> it = asignaturasNoAsignadas.iterator(); it.hasNext();) {
            Asignatura asignatura = it.next();
            jTable1.setValueAt(asignatura.getNombreAsignatura(), fila, 0);
            jTable1.setValueAt(asignatura.getIntensidadHoraria(), fila, 1);
            JComboBox comboBox = new JComboBox();
            comboBox.addItem("Ninguno");
            comboBox.setSelectedIndex(0);
            jTable1.setValueAt(comboBox, fila, 2);
            jTable1.setValueAt("---", fila, 3);
            for (Iterator<Laboratorio> it1 = laboratorios.iterator(); it1.hasNext();) {
                Laboratorio laboratorio = it1.next();
                if (laboratorio.getdisponibildaLab().equals("SI")){
                    comboBox.addItem(laboratorio.getnombreLab());
                }
            }
            fila++;
            comboBox.addActionListener(accion);
        }
    }
}
